#include "../views.hpp"


using lms::views::UserView;

UserView::UserView() {

    option = 0;
    userName = "";
    password = "";
    fullName = "";
    user = null;
    UnitOfWork& unitOfWork = UnitOfWork::getInstance();
    repository = unitOfWork.getUserRepository();

}

User* UserView::createUser() {
    Utilities::printText("\n\t\tUser General Information\n");
    userName = Utilities::reinterpret_input<string > ("\tUser Name: ");
    password = Utilities::reinterpret_input<string > ("\tPassword : ");
    fullName = Utilities::reinterpret_input<string > ("\tFull Name: ");
    user = new User(userName, password, fullName);
    return user;
}

void UserView::remove() {
    showUsers();
    option = Utilities::reinterpret_input<int>("\n\tEnter User ID: ");
    option--;
    repository->remove(option);
}

void UserView::edit() {
    showUsers();
    option = Utilities::reinterpret_input<int>("\n\tEnter User ID: ");
    option--;
    user = createUser();
    repository->edit(user, option);
}

void UserView::add() {
    user = createUser();
    repository->add(user);
}

void UserView::showUsers() {
    Node<User>* i = (repository->get())->getFirst();  
    int x = 1;
  Utilities::printText("\t Lists Of Users\n");
    while (i != NULL) {
      
        cout << "\tUser No." << x++ << "\n";
        cout << "\tUser Name: " << i->getData()->getUserName() << "\n";
        cout << "\tPassword: " << i->getData()->getPassword() << "\n";
        cout << "\tFull Name: " << i->getData()->getFullName() << "\n\n";
        i = i->getNext();
    }
}

void UserView::show() {

    do{
    Utilities::clearScreen();
    Utilities::printText("\t\t\tUser Management\n");
    Utilities::printText("\t1. Add User: \n");
    Utilities::printText("\t2. List All: \n");
    Utilities::printText("\t3. Delete User: \n");
    Utilities::printText("\t4. Modify User: \n");
    Utilities::printText("\t5. Exit: \n");
    option = Utilities::reinterpret_input<int>("\tChoose An Option: ");

    switch (option) {
        case 1:
            Utilities::clearScreen();
            add();
            createUser();
            break;

        case 2:
            Utilities::clearScreen();
            showUsers();
            Utilities::pause();
            break;
            
        case 3:
            Utilities::clearScreen();
            edit();
            break;

        case 4:
            Utilities::clearScreen();
            remove();
            break;
            
        case 5:
            break;
            
        default:
            Utilities::printText("\t Invalid Option\n");
            Utilities::pause();
            break;

    }
  }while(option != 5);
}
